package com.ahu.qifei.common.service.impl;

import com.ahu.qifei.common.bean.ResultModel;
import com.ahu.qifei.common.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.Objects;

/**
 * @author ShiQifei
 * @date 2020/11/21 16:18
 */
@Service
@Slf4j
public class YundaMailSerivceImpl implements IMailService {
    @Value("${yunda.mail.username}")
    String from;

    @Value("${yunda.mail.nick}")
    String nick;

    @Autowired
    @Qualifier("yundasys")
    private JavaMailSender mailSender;


    /**
     * 发送邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     * @return
     */
    @Override
    public ResultModel sendMessage(String[] to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
            mailSender.send(message);
        } catch (Exception e) {
            log.error("发送简单邮件时发生异常!", e.getMessage(), e);
            return ResultModel.fail(e.getMessage());
        }
        return ResultModel.success();
    }

    /**
     * 发送 HTML 邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public ResultModel sendMimeMessage(String[] to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = createHelper(message, to, subject, content);
        Objects.requireNonNull(helper, "helper为null");
        mailSender.send(message);
        return ResultModel.success();
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param filePath 附件路径
     */
    @Override
    public ResultModel sendMimeMessage(String[] to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = createHelper(message, to, subject, content);
            Objects.requireNonNull(helper, "helper为null");
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            helper.addAttachment(fileName, file);

            mailSender.send(message);
        } catch (MessagingException e) {
            log.error("发送带附件的MimeMessge时发生异常！", e.getMessage(), e);
            return ResultModel.fail(e.getMessage());

        }
        return ResultModel.success();

    }

    /**
     * 发送带静态文件的邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param rscIdMap 需要替换的静态文件
     */
    @Override
    public ResultModel sendMimeMessage(String[] to, String subject, String content, Map<String, String> rscIdMap) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = createHelper(message, to, subject, content);
            Objects.requireNonNull(helper, "helper为null");
            for (Map.Entry<String, String> entry : rscIdMap.entrySet()) {
                FileSystemResource file = new FileSystemResource(new File(entry.getValue()));
                helper.addInline(entry.getKey(), file);
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            log.error("发送带静态文件的MimeMessge时发生异常！", e.getMessage(), e);
            return ResultModel.fail(e.getMessage());
        }
        return ResultModel.success();
    }

    private MimeMessageHelper createHelper(MimeMessage message, String[] to , String subject, String content) {
        MimeMessageHelper helper = null;
        try {
            //true表示需要创建一个multipart message
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(nick+" <"+from+">"));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
        } catch (MessagingException e) {
            log.error("初始化helper异常", e.getMessage(), e);
        }
        return helper;
    }
}
